﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentValidation;
using Microhard.Protal.Models;
using Microhard.Protal.Interfaces;
using Microhard.Core;
using FluentValidation.Results;
using Microhard.Protal.Resource;

namespace Microhard.Protal.Validators
{
    public class ProductModelValidator : AbstractValidator<ProductModel>
    {
        private IProduct pd = IoC.Resolve<IProduct>();

        public ProductModelValidator()
        {
            RuleSet("Insert", () =>
            {
                RuleFor(u => u.Name).NotEmpty().WithMessage("请填写产品名称").When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.Name).Length(1, 50).WithMessage("产品名称的字符范围为1-50个字符").When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.Decription).NotEmpty().WithMessage("请填写描述").When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.ShortDescription).NotEmpty().WithMessage("请填写简述").When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                RuleFor(u => u.ShortDescription).Length(1, 50).WithMessage("简述的字符范围为1-50个字符").When(r => r.OperationStatus == Core.DBUtility.OperationStatus.Add);
                Custom(u =>
                {
                    if (pd.IsSKUDeplicate(u))
                    {
                        return new ValidationFailure("SKU", Msg.DuplicateSKU);
                    }
                    return null;
                });
            });


            RuleSet("Update", () =>
            {
                RuleFor(u => u.Name).NotEmpty().WithMessage("请填写产品名称");
                RuleFor(u => u.Name).Length(1, 50).WithMessage("产品名称的字符范围为1-50个字符");
                RuleFor(u => u.Decription).NotEmpty().WithMessage("请填写描述");
                RuleFor(u => u.ShortDescription).NotEmpty().WithMessage("请填写简述");
                RuleFor(u => u.ShortDescription).Length(1, 50).WithMessage("简述的字符范围为1-50个字符");
            });

        }
    }
}
